import { EnumObject } from '@/pages/routes/table/QueryForm';

const data = {
  displayFieldName: 'GID',
  fieldAliases: {
    GID: 'GID',
    AREA: 'AREA',
    TYPE: 'TYPE',
    LABEL: 'LABEL',
    COUNT: 'COUNT',
    COUNT_TYPE: 'COUNT_TYPE',
  },
  fields: [
    { name: 'GID', type: 'esriFieldTypeOID', alias: 'GID', length: 64 },
    {
      name: 'AREA',
      type: 'esriFieldTypeString',
      alias: 'AREA',
      length: 64,
    },
    { name: 'TYPE', type: 'esriFieldTypeDouble', alias: 'TYPE', length: 64 },
    {
      name: 'LABEL',
      type: 'esriFieldTypeString',
      alias: 'LABEL',
      length: 64,
    },
    { name: 'COUNT', type: 'esriFieldTypeDouble', alias: 'COUNT', length: 64 },
    {
      name: 'COUNT_TYPE',
      type: 'esriFieldTypeDouble',
      alias: 'COUNT_TYPE',
      length: 64,
    },
  ],
  features: [
    {
      attributes: {
        GID: '1',
        AREA: '市管',
        TYPE: '1',
        LABEL: '管网总长',
        COUNT: '712',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '2',
        AREA: '市管',
        TYPE: '2',
        LABEL: '管网总长',
        COUNT: '710',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '3',
        AREA: '市管',
        TYPE: '1',
        LABEL: '市政管网总长',
        COUNT: '289',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '4',
        AREA: '市管',
        TYPE: '2',
        LABEL: '市政管网总长',
        COUNT: '220',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '5',
        AREA: '市管',
        TYPE: '1',
        LABEL: '小区管网总长',
        COUNT: '422',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '6',
        AREA: '市管',
        TYPE: '2',
        LABEL: '小区管网总长',
        COUNT: '494',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '7',
        AREA: '市管',
        TYPE: '1',
        LABEL: '小型管道(DN<600)',
        COUNT: '533',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '8',
        AREA: '市管',
        TYPE: '1',
        LABEL: '中型管道(600<=DN<1000)',
        COUNT: '113',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '9',
        AREA: '市管',
        TYPE: '1',
        LABEL: '大型管道(1000<=DN<1500)',
        COUNT: '54',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '10',
        AREA: '市管',
        TYPE: '1',
        LABEL: '特大型管道(DN>=1500)',
        COUNT: '12',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '11',
        AREA: '市管',
        TYPE: '2',
        LABEL: '小型管道(DN<600)',
        COUNT: '601',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '12',
        AREA: '市管',
        TYPE: '2',
        LABEL: '中型管道(600<=DN<1000)',
        COUNT: '83',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '13',
        AREA: '市管',
        TYPE: '2',
        LABEL: '大型管道(1000<=DN<1500)',
        COUNT: '21',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '14',
        AREA: '市管',
        TYPE: '2',
        LABEL: '特大型管道(DN>=1500)',
        COUNT: '5',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '15',
        AREA: '市管',
        TYPE: '1',
        LABEL: '检查井',
        COUNT: '30681',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '16',
        AREA: '市管',
        TYPE: '1',
        LABEL: '截流井',
        COUNT: '3',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '17',
        AREA: '市管',
        TYPE: '1',
        LABEL: '溢流井',
        COUNT: '2',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '18',
        AREA: '市管',
        TYPE: '1',
        LABEL: '闸门井',
        COUNT: '0',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '19',
        AREA: '市管',
        TYPE: '1',
        LABEL: '化粪池',
        COUNT: '31',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '20',
        AREA: '市管',
        TYPE: '1',
        LABEL: '净化池',
        COUNT: '2',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '21',
        AREA: '市管',
        TYPE: '1',
        LABEL: '隔油池',
        COUNT: '0',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '22',
        AREA: '市管',
        TYPE: '1',
        LABEL: '调蓄池',
        COUNT: '0',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '23',
        AREA: '市管',
        TYPE: '1',
        LABEL: '雨篦',
        COUNT: '39621',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '24',
        AREA: '市管',
        TYPE: '1',
        LABEL: '阀门',
        COUNT: '3',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '25',
        AREA: '市管',
        TYPE: '1',
        LABEL: '排水口',
        COUNT: '125',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '26',
        AREA: '市管',
        TYPE: '1',
        LABEL: '进水口',
        COUNT: '11',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '27',
        AREA: '市管',
        TYPE: '1',
        LABEL: '节点',
        COUNT: '16057',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '28',
        AREA: '市管',
        TYPE: '1',
        LABEL: '液位监测点',
        COUNT: '21',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '29',
        AREA: '市管',
        TYPE: '2',
        LABEL: '检查井',
        COUNT: '51997',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '30',
        AREA: '市管',
        TYPE: '2',
        LABEL: '截流井',
        COUNT: '12',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '31',
        AREA: '市管',
        TYPE: '2',
        LABEL: '溢流井',
        COUNT: '1',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '32',
        AREA: '市管',
        TYPE: '2',
        LABEL: '闸门井',
        COUNT: '0',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '33',
        AREA: '市管',
        TYPE: '2',
        LABEL: '化粪池',
        COUNT: '2573',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '34',
        AREA: '市管',
        TYPE: '2',
        LABEL: '净化池',
        COUNT: '29',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '35',
        AREA: '市管',
        TYPE: '2',
        LABEL: '隔油池',
        COUNT: '4',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '36',
        AREA: '市管',
        TYPE: '2',
        LABEL: '调蓄池',
        COUNT: '0',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '37',
        AREA: '市管',
        TYPE: '2',
        LABEL: '污篦',
        COUNT: '3453',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '38',
        AREA: '市管',
        TYPE: '2',
        LABEL: '阀门',
        COUNT: '3',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '39',
        AREA: '市管',
        TYPE: '2',
        LABEL: '排水口',
        COUNT: '91',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '40',
        AREA: '市管',
        TYPE: '2',
        LABEL: '进水口',
        COUNT: '84',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '41',
        AREA: '市管',
        TYPE: '2',
        LABEL: '节点',
        COUNT: '64615',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '42',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '管网总长',
        COUNT: '608',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '43',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '管网总长',
        COUNT: '472',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '44',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '管网总长',
        COUNT: '24',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '45',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '市政管网总长',
        COUNT: '261',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '46',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '市政管网总长',
        COUNT: '162',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '47',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '市政管网总长',
        COUNT: '3',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '48',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '小区管网总长',
        COUNT: '325',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '49',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '小区管网总长',
        COUNT: '258',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '50',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '小区管网总长',
        COUNT: '7',
        COUNT_TYPE: '1',
      },
    },
    {
      attributes: {
        GID: '51',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '小型管道(DN<600)',
        COUNT: '425',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '52',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '中型管道(600<=DN<1000)',
        COUNT: '153',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '53',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '大型管道(1000<=DN<1500)',
        COUNT: '27',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '54',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '特大型管道(DN>=1500)',
        COUNT: '3',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '55',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '小型管道(DN<600)',
        COUNT: '387',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '56',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '中型管道(600<=DN<1000)',
        COUNT: '64',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '57',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '大型管道(1000<=DN<1500)',
        COUNT: '21',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '58',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '特大型管道(DN>=1500)',
        COUNT: '0',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '59',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '小型管道(DN<600)',
        COUNT: '22',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '60',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '中型管道(600<=DN<1000)',
        COUNT: '1',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '61',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '大型管道(1000<=DN<1500)',
        COUNT: '0',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '62',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '特大型管道(DN>=1500)',
        COUNT: '0',
        COUNT_TYPE: '2',
      },
    },
    {
      attributes: {
        GID: '63',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '小泵井',
        COUNT: '8',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '64',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '阀门井',
        COUNT: '1',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '65',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '检查井',
        COUNT: '33224',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '66',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '雨水箅',
        COUNT: '427',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '67',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '闸坝',
        COUNT: '1',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '68',
        AREA: '淮安区',
        TYPE: '1',
        LABEL: '其他',
        COUNT: '12068',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '69',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '小泵井',
        COUNT: '2',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '70',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '阀门井',
        COUNT: '0',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '71',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '检查井',
        COUNT: '22207',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '72',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '雨水箅',
        COUNT: '28759',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '73',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '闸坝',
        COUNT: '1',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '74',
        AREA: '淮安区',
        TYPE: '2',
        LABEL: '其他',
        COUNT: '12352',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '75',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '小泵井',
        COUNT: '1',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '76',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '阀门井',
        COUNT: '0',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '77',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '检查井',
        COUNT: '1513',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '78',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '雨水箅',
        COUNT: '1215',
        COUNT_TYPE: '3',
      },
    },
    {
      attributes: {
        GID: '79',
        AREA: '淮安区',
        TYPE: '3',
        LABEL: '闸坝',
        COUNT: '0',
        COUNT_TYPE: '3',
      },
    },
    { attributes: { GID: '80', AREA: '淮安区', TYPE: '3', LABEL: '其他', COUNT: '1331', COUNT_TYPE: '3' } },
  ],
};

interface A {
  GID: string;
  AREA: string;
  TYPE: string;
  LABEL: string;
  COUNT: string;
  COUNT_TYPE: string;
}

interface CountObject {
  id: number;
  area: string;
  type: number;
  label: string;
  count: number;
}


interface BaseObject {
  label: string;
  unit: string;
  // icon: string;
}

interface EnumObject<T = string> {
  label: string;
  value: T;
}


function getCountData(){

  const recs: { attributes: A }[] = data.features;
  const result = recs.reduce<Record<string, CountObject[]>>((acc, c) => {
    const { GID, AREA, TYPE, COUNT, COUNT_TYPE, LABEL } = c.attributes;
    if (!acc[COUNT_TYPE]) {
      acc[COUNT_TYPE] = [];
    }
    acc[COUNT_TYPE].push({
      id: Number(GID),
      area: AREA,
      type: Number(TYPE),
      label: LABEL,
      count: Number(COUNT),
    });
    return acc;
  }, {});

  function getMapMap() {
    const lenConfig: Record<string, BaseObject> = {
      1: {
        label: '污水',
        unit: 'km',
        // icon: ImgWaste,
      },
      2: {
        label: '雨水',
        unit: 'km',
        // icon: ImgRain,
      },
      3: {
        label: '合流',
        unit: 'km',
        // icon: ImgRain,
      },
    };
    const lm = result[1].reduce<Record<string, CountObject[]>>((acc, c) => {
      if (!acc[c.area]) {
        acc[c.area] = [];
      }
      acc[c.area].push(c);
      return acc;
    }, {});
    const mapp = Object.keys(lm).reduce<Record<string, any[]>>((acc, c) => {
      const arr = lm[c];
      const data = arr.reduce<Record<string, CountObject[]>>((acc, c) => {
        if (!acc[c.label]) {
          acc[c.label] = [];
        }
        acc[c.label].push(c);
        return acc;
      }, {});
      return {
        ...acc,
        [c]: Object.keys(data).map(k => {
          const v = data[k];
          return {
            label: k,
            value: (v.reduce<number>((acc, c) => acc + c.count, 0) / 1000).toFixed(3),
            unit: 'km',
            // icon: ImgNet,
            cates: v.map(vv => ({
              ...lenConfig[vv.type],
              value: (vv.count / 1000).toFixed(3),
            })),
          };
        }),
      };
    }, {});
    console.log(mapp);
    return mapp;
  }
  function getMapMap2() {
    const lm = result[2].reduce<Record<string, CountObject[]>>((acc, c) => {
      if (!acc[c.area]) {
        acc[c.area] = [];
      }
      acc[c.area].push(c);
      return acc;
    }, {});
    const mapp = Object.keys(lm).reduce<Record<string, any[]>>((acc, c) => {
      const arr = lm[c];
      const data = arr.reduce<Record<string, CountObject[]>>((acc, c) => {
        if (!acc[c.label]) {
          acc[c.label] = [];
        }
        acc[c.label].push(c);
        return acc;
      }, {});
      return {
        ...acc,
        [c]: Object.keys(data).map(k => {
          const v = data[k];
          return {
            label: k,
            value: (v.reduce<number>((acc, c) => acc + c.count, 0) / 1000).toFixed(3),
            unit: 'km',
            // icon: ImgNet,
          };
        }),
      };
    }, {});
    console.log(mapp);
    return mapp;
  }
  function getMapMap3() {
    const lm = result[3].reduce<Record<string, EnumObject<number>[]>>((acc, c) => {
      if (!acc[c.area]) {
        acc[c.area] = [];
      }
      acc[c.area].push({
        label: c.label,
        value: c.count,
      });
      return acc;
    }, {});
    return lm;
  }

  return {

  }
}

